package com.Archive.firstWeek;

import java.util.Scanner;

public class Split_character {

    //将字符串转换成char数组
    public char[] character(String str) {
        char[] chararr = str.toCharArray();
        return chararr;
    }

    //将char数组进行加密
    public int[] encryption(char[] cacheChar) {
        int[] cacheInt = new int[cacheChar.length];
        for (int i = 0; i < cacheChar.length; i++) {
            cacheInt[i] = 126 - cacheChar[i] + 33;
        }
        return cacheInt;
    }
    //将int数组进行解密
    public int[] unencryption(int[] cacheInt) {
        int[] cacheChar = new int[cacheInt.length];
        for (int i = 0; i < cacheInt.length; i++) {
            cacheChar[i] = (126 + 33) - cacheInt[i];
        }
        return cacheChar;
    }

    //将ASCII码转换成char输出
    public void display(int[] display,String info){
        System.out.print(info + "信息为：");
        for(int a:display){                         //将ASCII码转换成字符进行打印
            System.out.print((char)a);
        }
        System.out.println("");
    }

    public static void main(String[] args) {
        Split_character m = new Split_character();  //引入 Split_character
        Scanner input = new Scanner(System.in);     //引入 Scanner
        System.out.println("请输入需要加密的信息：");
        String inputString = input.nextLine();      //输入需要加密的信息
        char[] inputChar = m.character(inputString);    //将需要加密的信息拆分成字符数组
        int[] encrypedInt = m.encryption(inputChar);    //将加密字符数组转换成ASCII码
        m.display(encrypedInt,"加密");
        int[] unencrypedInt = m.unencryption(encrypedInt);
        m.display(unencrypedInt,"解密");
    }
}
